/**
 * 
 */
package front_shop;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.swing.*;
import javax.swing.table.JTableHeader;


/**
 * @author wx
 *
 */
@SuppressWarnings("serial")
public class replenish extends JFrame implements ActionListener{

	/**
	 * @param args
	 */
	JPanel panel;
	JLabel bh,shuliang,danjia;
	JTextField spbh,sl,dj;
	JTable table;
	JButton delete,ok,neww;
	Object t[][]= new Object[16][3];
	Connection con;
    Statement sql; 
    ResultSet rs;
	int i=0;
	static String  e="";
	static String  f="";
	boolean flag=false;
	public static void s(String a,String b ){
		e=a;
		f=b;
	}
	public replenish(){
		panel=new JPanel();
		bh=new JLabel("商品编号");
		shuliang=new JLabel("数量");
		danjia=new JLabel("单价");
		spbh=new JTextField(20);
		sl=new JTextField(20);
		dj=new JTextField(20);
		delete=new JButton("删除");
		ok=new JButton("提交");
		neww=new JButton("新增商品");
		
		panel.setLayout(null);
		this.add(panel);
		panel.add(bh);
		bh.setBounds(20,20,80,20);
		panel.add(spbh);
		spbh.setBounds(80,20,80,20);
		panel.add(shuliang);
		shuliang.setBounds(210,20,80,20);
		panel.add(sl);
		sl.setBounds(240,20,80,20);
		panel.add(neww);
		neww.setBounds(370,20,100,20);
	//	panel.add(danjia);
	//	danjia.setBounds(360,20,80,20);
	//	panel.add(dj);
	//	dj.setBounds(390,20,80,20);
		panel.add(delete);
		delete.setBounds(120,330,80,20);
		panel.add(ok);
		ok.setBounds(300,330,80,20);
		
		Object columnname[]={"ID","商品编号","采购数量"};
		table=new JTable(t,columnname);
		JTableHeader tableheader=table.getTableHeader();
		panel.add(tableheader);
		panel.add(table);
		tableheader.setBounds(20,50,460,20);
		table.setBounds(20,70,460,250);
		
		spbh.addActionListener(this);
		sl.addActionListener(this);
//		dj.addActionListener(this);
		ok.addActionListener(this);
		neww.addActionListener(this);
		delete.addActionListener(this);
		sl.enable(false);
//		dj.enable(false);
		
		try{  
		    Class.forName("com.mysql.jdbc.Driver");
		    }
	 	catch(Exception e){
		       System.out.println("忘记把MySQL数据库的JDBC-数据库驱动程序复制到JDK的扩展目录中");
		    }
	 	try { String uri= "jdbc:mysql://127.0.0.1:3306/javashop";
		          String user="root";
		          String ps="123";
		          con=DriverManager.getConnection(uri,user,ps);
		          sql=con.createStatement();
		    }
	    catch(SQLException e){ 
		          System.out.println(e);
		    }
		
		
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		replenish user=new replenish();
		user.setTitle("进货 ");
		user.setBounds(200,200,500,400);
		user.setVisible(true);
		user.validate();
		user.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent a) {
		// TODO Auto-generated method stub
		if (a.getSource()==delete){
			int q,j;
			q=table.getSelectedRow();
			if (q!=-1){
				for ( j=0;j<3;j++){
					t[q][j]="";
				}
			}
			for(j=q;j<i;j++){
				for (int n=0;n<3;n++){
					t[j][n]=t[j+1][n];
				}
			}
			for ( j=0;j<3;j++){
				t[i][j]="";
			}
			i--;
			table.repaint();
			spbh.setText("");
			sl.setText("");
			spbh.enable(true);
			sl.enable(false);
			spbh.requestFocus();
		}
		else if (a.getSource()==neww){
			flag=true;
			neww user=new neww();
			user.setTitle("新增商品 ");
			user.setBounds(200,200,500,170);
			user.setVisible(true);
			user.validate();
			user.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			sl.enable(true);
		}
		else if (a.getSource()==ok){//
			//新增商品应该立刻需改stock表和商品表，发生在提交按钮之前
			if(sl.isEnabled()==false){
				for (int j=0;j<i;j++){
					System.out.println(j);
					
				try{
				double x=0;
				rs=sql.executeQuery("SELECT * FROM stock where MerchandiseNumber=\""+t[j][1].toString()+"\"");
				if(rs.next()==true){
					 int amount=Integer.parseInt(rs.getString(3))+Integer.parseInt(t[j][2].toString());
					// System.out.println(amount);
					 String w="update stock set number="+amount+" where MerchandiseNumber=\""+t[j][1].toString()+"\"";
					 int ok=sql.executeUpdate(w);
					 System.out.println(amount);
					 
					 //更新流水表
					Date nowTime=new Date();
					String time=String.format("%tY-%<tm-%<td", nowTime);
					rs=sql.executeQuery("SELECT * FROM merchandise where MerchandiseNumber=\""+t[j][1].toString()+"\"");
					rs.next();						// 0607 改表名字 👆
					x=Double.parseDouble(rs.getString(6));//该商品单价
					rs=sql.executeQuery("SELECT * FROM cashbalance where date=\""+time+"\"");
					 if(rs.next()==true){//更新
						 Double y=0.0;
						 if (rs.getString(3)!=null)
							 y=Double.parseDouble(rs.getString(3));
						 x=y+x*Integer.parseInt(t[j][2].toString());
						 w="update cashbalance set expend="+x+" where date=\""+time+"\"";
						 ok=sql.executeUpdate(w);
					 }
					 else
					 {	x=x*Integer.parseInt(t[j][2].toString());
						w="insert into cashbalance (date,expend) values ( \""+time+"\","+x+")";
						ok=sql.executeUpdate(w);
					 }
					 //更新进货单
					rs=sql.executeQuery("SELECT * FROM purchase where date=\""+time+"\" and MerchandiseNumber=\""+t[j][1].toString()+"\" ");
					if(rs.next()==true){//更新
						x=Double.parseDouble(rs.getString(3))+Integer.parseInt(t[j][2].toString());
						 w="update purchase set amount="+x+" where date=\""+time+"\"and MerchandiseNumber=\""+t[j][1].toString()+"\" ";
						 ok=sql.executeUpdate(w);
					}
					else{
						x=Integer.parseInt(t[j][2].toString());
						w="insert into purchase  values ( \""+time+"\",\""+t[j][1].toString()+"\","+x+")";
					 ok=sql.executeUpdate(w);

					}
					 
					

				 }
				 else 
					 JOptionPane.showMessageDialog(this,"更新库存表失败！！！","进货",JOptionPane.WARNING_MESSAGE);
				}
				
				catch(SQLException a1){
			          System.out.println(a1);
				}
				}
				 JOptionPane.showMessageDialog(this,"进货成功！！！","进货",JOptionPane.WARNING_MESSAGE);
				 
				 int m,n;
					for (m=0;m<16;m++){
						for(n=0;n<3;n++){
							t[m][n]="";
						}
					}
				spbh.setText("");
				spbh.requestFocus();
				sl.setText("");
				sl.enable(false);
				spbh.enable(true);
				i=0;
				table.repaint();

			}
			else{
				sl.requestFocus();
				JOptionPane.showMessageDialog(this,"请输入数量！！！","进货",JOptionPane.WARNING_MESSAGE);
			}
		}
		else if (a.getSource()==spbh){
			if (spbh.getText().length()==7){ 
				try{
					 rs=sql.executeQuery("SELECT * FROM stock where MerchandiseNumber=\""+spbh.getText()+"\"");
					 if( rs.next()==true){//找到该商品
						 t[i][0]=rs.getString(1);
						 t[i][1]=rs.getString(2);
						 table.repaint();
						 sl.enable(true);
						 sl.requestFocus();
						 spbh.enable(false);
					 }
					 else //在商品表里没有找到该商品
					 { JOptionPane.showMessageDialog(this,"商品编号输入错误！！！","进货",JOptionPane.WARNING_MESSAGE);
					 spbh.requestFocus();
					 spbh.setText("");
					 }
				}
				  catch(SQLException c){ 
			          System.out.println(c);
			    }
			}
		}
		else if (a.getSource()==sl){
			if (flag==true){
				t[i][0]=e;
				t[i][1]=f;
				System.out.println("dasf"+e);
				sl.enable(true);
				spbh.setText(t[i][0].toString());
				sl.requestFocus();
				table.repaint();
				flag=false;
			}
			if (!sl.getText().equals("")&&Integer.parseInt(sl.getText())>0 ){//输入的数量大于0
				 t[i][2]=sl.getText();
				 table.repaint();
				 spbh.setText("");
				 spbh.enable(true);
				 spbh.requestFocus();
				 sl.setText("");
				 sl.enable(false);
				 i++;
			}
			else
				 {JOptionPane.showMessageDialog(this,"数量必须大于零！！！","进货",JOptionPane.WARNING_MESSAGE);
				 sl.setText("");
				 sl.requestFocus();
				 }

		
		}
	
	
		
		
	}

}


 